const Koa = require('koa');

const app = new Koa();

const bodyParser = require('koa-bodyparser'); // koa中间键可以直接通过request.body获取post值

app.use(bodyParser());

app.use(async (ctx) => {
    if (ctx.url === '/' && ctx.method === 'GET') {
        let html = `
        <h1>demo3 request Post</h1>
        <form method="POST" action="/">
            <p>userName</p> <br />
            <input name="userName" /> <br/>
            <p>age</p> <br/>
            <input name="age"/> <br />
            <p>webSite</p> <br/>
            <input name="webSite" /> <br/>
            <button type="submit">submit</button>
        </form>
        `
        ctx.body = html
    } else if (ctx.url === '/' && ctx.method === 'POST') {
        let postData = ctx.request.body;
        ctx.body = postData;
    } else {
        ctx.body = '<h1>404</h1>'
    }
})

app.listen(3000,()=>{
    console.log('[demo] server is starting at port 3000');
});